﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity;
using Yyyg.IDAL;
using Yyyg.Models;

namespace Yyyg.DAL
{
    public abstract class BaseDAL<T> : IBaseDAL<T> where T : class, new()
    {
        private LuckyEntities context = DbContextFactory.DbLucky;
        private DbSet<T> dbset;
        public BaseDAL()
        {
            dbset = context.Set<T>();
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="t"></param>
        public virtual void Add(T t)
        {
            dbset.Add(t);
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="t"></param>
        public virtual void Del(T t)
        {
            dbset.Remove(t);
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="t"></param>
        public virtual void Update(T t)
        {
            dbset.Attach(t);
        }

        /// <summary>
        /// 返回所有
        /// </summary>
        /// <returns></returns>
        public IQueryable<T> GetAll()
        {
            return dbset;
        }

        /// <summary>
        /// 返回所有(异步执行)
        /// </summary>
        /// <returns></returns>
        public async Task<IQueryable<T>> GetAllAsync()
        {
            return await Task.Run(GetAll);
        }

        /// <summary>
        /// 查询若干数据
        /// </summary>
        /// <param name="wherelambda"></param>
        /// <returns></returns>
        public IQueryable<T> GetModels(Expression<Func<T, bool>> wherelambda)
        {
            return dbset.Where(wherelambda);
        }

        /// <summary>
        /// 返回指定页指定数量的数据
        /// </summary>
        /// <param name="size">页大小</param>
        /// <param name="index">页码,从0开始</param>
        public IQueryable<T> GetModelsByPage(int size, int index)
        {
            return dbset.Skip((index - 1) * size).Take(size);
        }

        /// <summary>
        /// 返回经筛选后,指定页指定数量的数据
        /// </summary>
        /// <param name="size">页大小</param>
        /// <param name="index">页码,从0开始</param>
        /// <param name="whereLambda">where查询表达式</param>
        public IQueryable<T> GetModelsByPage(int size, int index, Expression<Func<T, bool>> whereLambda)
        {
            return dbset.Where(whereLambda).Skip((index - 1) * size).Take(size);
        }

        /// <summary>
        /// 返回经筛选后,指定页指定数量并排序后的数据
        /// </summary>
        /// <param name="size">页大小</param>
        /// <param name="index">页码,从0开始</param>
        /// <param name="whereLambda">where查询表达式</param>
        /// <param name="orderbyLambda">排序关键字</param>
        /// <param name="isAsc">是否升序,默认为true</param>
        public IQueryable<T> GetModelsByPage(int size, int index, Expression<Func<T, bool>> whereLambda, Expression<Func<T, Type>> orderbyLambda, bool isAsc = true)
        {
            if (isAsc) return dbset.Where(whereLambda).Skip((index - 1) * size).Take(size).OrderBy(orderbyLambda);
            else return dbset.Where(whereLambda).Skip((index - 1) * size).Take(size).OrderByDescending(orderbyLambda);
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <returns></returns>
        public bool SaveChanges()
        {
            return this.context.SaveChanges() > 1;
        }

        /// <summary>
        /// 返回页数
        /// </summary>
        /// <param name="size"></param>
        /// <returns></returns>
        public int GetPageCount(int size)
        {
            int rem = dbset.Count() % size;
            if (rem > 0) rem++;
            return rem;
        }

        /// <summary>
        /// 异步保存
        /// </summary>
        /// <returns></returns>
        public async Task<bool> SaveChangesAsync()
        {
            bool res = await this.context.SaveChangesAsync()>0;
            return res;
        }
    }
}
